package com.dkm.nettysocketdemo.myWebsite.action;

import com.dkm.nettysocketdemo.myWebsite.domain.HelmetUser;
import com.dkm.nettysocketdemo.myWebsite.service.HelmetUserService;
import com.dkm.nettysocketdemo.untils.Message;
import com.dkm.nettysocketdemo.untils.Tools;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  @ClassName : HelmetUserController  //类名
 *  @Description : 客户  //描述
 *  @Author : 石智诚  //作者
 *  @Date: 2021-03-19 10:58  //时间
 */
@RestController
@RequestMapping("/HelmetUser")
public class HelmetUserAction {
    /**
     * 客户服务接口
     */
    @Resource
    HelmetUserService service;
    /**
     * 后台登录人
     */
    HelmetUser helmetUser;


    /**
     * 后台登录
     * @param userAccountNumber 用户账号
     * @param userPwd 密码
     * @param session
     * @return Message
     */
    @GetMapping("/userLogin")
    public Message userLogin(String userAccountNumber, String userPwd,HttpSession session){
        HelmetUser login = service.isLogin(userAccountNumber, userPwd);
        Message message = new Message();
        message.setCode(200);
        if (ObjectUtils.isEmpty(login)) {
            message.setData(0);
            message.setMsg("登录失败");
        }else {
            session.setAttribute("user",login);
            message.setData(1);
            message.setMsg("登录成功");
        }
        return  message;
    }

    /**
     * 根据id查询
     * @param id 用户id
     * @return Message
     */
    @GetMapping("/findByid/{id}")
    public Message findById(@PathVariable("id") String id){
        Message message=new Message();
        HelmetUser byId = service.findById(id);
        message.setCode(200);
        message.setData(byId);
        return  message;
    }

    /**
     * 查询所有的用户
     * @param startTime  开始时间
     * @param endTime  结束时间
     * @param page   页面
     * @param limit 显示的数量
     * @param user 用户
     * @return
     */
    @GetMapping("/userSelectAll")
    public Message userSelectAll(String startTime, String endTime, Integer page, Integer limit,HelmetUser user){
        Message message=new Message();
        List<HelmetUser> helmetUsers = service.selectAll(startTime, endTime, page, limit, user);
        Integer count = service.rowCount(startTime, endTime, user);
        message.setCount(count);
        message.setData(helmetUsers);
        message.setCode(0);
        return message;
    }

    /**
     * 增加用户
     * @param user 用户
     * @return Message
     */
    @PostMapping("/addOrUpdateUser")
    public Message addUser(HelmetUser user,HttpServletRequest request){
        rowMap(request);
            Message message=new Message();
            int row;
            if (user.getUserId().isEmpty()){
                user.setCreatedBy(helmetUser.getUserName());
                row=service.insert(user);
            }else {
                user.setUpdatedBy(helmetUser.getUserName());
                row=service.update(user);
            }
            message.setCode(200);
            if (row>0){
                message.setCount(row);
                message.setMsg("操作成功");
            }else {
                // 更新失败
                message.setCount(0);
                message.setMsg("操作失败");
            }
        return message;
        }



    /**
     * 将安全帽设置为禁用
     * @param userId 用户id
     * @return Message
     */
    @PostMapping("userDeletById/{userId}")
    public Message  userDeletById(@PathVariable("userId") String userId,HttpServletRequest request){
        Message message=new Message();
        rowMap(request);
        int update = service.updateById(userId,helmetUser.getUserName());
        message.setCode(200);
        if (update>0){
            message.setCount(update);
            message.setData("操作成功");
            return message;
        }else {
            message.setCount(update);
            message.setData("操作失败");
            return message;
        }
    }



    /**
     * 批量删除
     * @param ids 安全帽id
     * @return Message
     */
    @PostMapping("/userDeletAll")
    public Message userDeletAll(@RequestParam(value = "ids[]", required = false) String[] ids,HttpServletRequest request){
        rowMap(request);
        Message message=new Message();
        List<String> idssi = null;
        //id为空的情况下
        if (ObjectUtils.isEmpty(ids)) {
            message.setMsg("操作失败");
            message.setCode(0);
        } else {
            idssi = Arrays.asList(ids);
        }
        int row = service.updateByAll(idssi,helmetUser.getUserName());
        if (row>0){
            message.setMsg("操作成功");
            message.setCode(1);
            message.setCount(row);
        }else {
            message.setMsg("操作失败");
            message.setCode(0);
        }
        return  message;
    }
    /**
     * 修改密码
     *
     * @param newUserPwd     新密码
     * @param oldUserPwd     旧密码
     * @param confirmUserPwd 确认密码
     * @return 回调信息
     */
    @PostMapping("updateUserPwd")
    public Message updateUserPwd(String newUserPwd, String oldUserPwd, String confirmUserPwd,HttpServletRequest request) {
        rowMap(request);
        Message  message = new Message();
        HelmetUser user = service.findById(helmetUser.getUserId());
        //判断旧密码是否是不是一样的
        if (user.getUserPwd().equals(Tools.password(oldUserPwd)) && confirmUserPwd.equals(newUserPwd)) {
            user.setUserPwd(newUserPwd);
            int row = service.update(user);
            if (row == 1) {
                message.setCount(row);
                message.setMsg("修改成功");
            } else {
                message.setCount(0);
                message.setMsg("修改失败");
            }
        } else {
            message.setCount(0);
            message.setMsg("旧密码不正确或确认密码错误");
        }
        return message;
    }

    /**
     * 查询客户的账号是否重名
     * @param userAccountNumber 客户账号
     * @param userId 客户id
     * @return Message
     */
    @GetMapping("/userSelectCount")
    public Message selectCount(String userAccountNumber,String userId){
        Message message=new Message();
        Integer count = service.selectCount(userAccountNumber, userId);
        message.setCode(200);
        message.setData(count);
        return  message;
    }
    /**
     * 注销
     *
     * @param session 用户的信息
     * @return 1
     */
    @GetMapping("/logout")
    public int logout(HttpSession session) {
        session.removeAttribute("user");
        return 1;
    }
    /**
     * 获得user的信息
     * @param session 登录的信息
     * @return 人员模型
     */
    @GetMapping("getSeeion")
    public Map<String, Object> getSeeion(HttpSession session) {
        Map<String, Object> map = new HashMap<>(16);
       HelmetUser user=(HelmetUser) session.getAttribute("user");
        if (ObjectUtils.isEmpty(user)) {
            map.put("code", 0);
        } else {
            map.put("code", 1);
        }
        return map;
    }

    /**
     * 获得用户信息
     *
     * @param session 操作人的信息
     * @return 用户信息
     */
    @GetMapping("getUser")
    public HelmetUser getUser(HttpSession session) {
        HelmetUser user= (HelmetUser) session.getAttribute("user");
        if (ObjectUtils.isEmpty(user)) {
            user=new HelmetUser();
            user.setUserName("未登录");
            user.setIsRevision("9");
        }
        return user;
    }
    /**
     * 简化代码
     *
     * @param request 请求
     */
    private void rowMap(HttpServletRequest request) {
         helmetUser = (HelmetUser) request.getSession().getAttribute("user");
    }
}
